import {getToken} from '@/request/token'

import Vue from 'vue'
import VueRouter from 'vue-router'


Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    name: 'index',
    component:resolve=>require(['@/views/home/Index'],resolve)
  },
  {
    path: '/login',
    name: 'Login',
    component:resolve=>require(['@/views/login/Login'],resolve)
  },
  {
    path: '/write',
    name: 'write',
    component:resolve=>require(['@/views/blog/BlogWriter'],resolve)

  },

  {
    path: '/view/:id',
    name: 'view',
    component:resolve=>require(['@/views/post/Post'],resolve)

  },
  {
    path: '/archives',
    name: 'archives',
    component:resolve=>require(['@/views/archive/Archive'],resolve)

  },
  {
    path: '/categories',
    name: 'categories',
    component:resolve=>require(['@/views/category/index'],resolve)

  },
  {
    path: '/tags',
    name: 'tags',
    component:resolve=>require(['@/views/tag/index'],resolve)

  },
  {
    path: '/about',
    name: 'about',
    component:resolve=>require(['@/views/about/Index'],resolve)

  },

];

const router = new VueRouter({
  routes
});


const authUrlList = ['/write']
const whiteList = ['/login', '/']; // 不重定向白名单

router.beforeEach((to, from, next) => {

  if (authUrlList.indexOf(to.path) !== -1) {

    const $store = router.app.$options.store || '';
    const state = $store.state || '';

    if (getToken()) {
      next(authUrlList)
      /*  if (to.path === '/login') {
          next(authUrlList)
        } else {
          if (!state.account || state.account.length === 0) {
            $store.dispatch('getUserInfo').then(data => { //获取用户信息
              next()
            }).catch(() => {
              next({path: '/login'}
              )
            })
          } else {
            next()
          }
        }*/
    } else {
      next('/login');
      NProgress.done()
    }

  } else {
    next()
  }


});

router.afterEach((to, from, next) => {
  window.scrollTo(0, 0);
});

export default router
